package controler;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.GetCnn;

import model.SanphamModel;

/**
 * Servlet implementation class dangky
 */
@WebServlet("/dangky")
public class dangky extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public dangky() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @throws SQLException 
	 * @throws IOException 
	 * @throws ServletException 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException{
    	response.setContentType("text/html;charset=UTF-8");
    	Connection cnn = GetCnn.getCnn();
		Statement stat = cnn.createStatement();
    	String fldmember_login = "";
    	String fldmember_password = "";
    	String fldhoten = "";
    	String fldngaysinh = "";
    	String flddiachi = "";
    	String flddienthoai = "";
    	String fldemail="";

    	fldmember_login = request.getParameter("member_login");
    	fldmember_password = request.getParameter("member_password");
    	fldhoten = request.getParameter("hoten");
    	fldhoten = new String(fldhoten.getBytes("8859_1"),"UTF8");
    	fldngaysinh = request.getParameter("ngaysinh");
    	flddiachi = request.getParameter("diachi");
    	flddiachi = new String(flddiachi.getBytes("8859_1"),"UTF8");
    	flddienthoai = request.getParameter("dienthoai");
    	fldemail = request.getParameter("email");
    	
    	SanphamModel model = new SanphamModel();
    	
    	int sodong = model.getTotalRows("select * from KHACHHANG");
    	String sSQL = "insert into KHACHHANG(MAKH,TENDANGNHAP,MATKHAU,HOTEN,NGAYSINH,DIACHI,DIENTHOAI,EMAIL,NGAYDK) values ("+sodong+",'"+fldmember_login+"','"+fldmember_password+"',N'"+fldhoten+"','"+fldngaysinh+"',N'"+flddiachi+"','"+flddienthoai+"','"+fldemail+"',GETDATE())";
    	stat.executeUpdate(sSQL);
    	RequestDispatcher view = request.getRequestDispatcher("luudangky.jsp");
        view.forward(request, response);

    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
